package com.fenqing.leetcode.code.t0_200.t100_200;

import com.fenqing.annotations.DesignClass;
import com.fenqing.core.design.DesignTopic;

import java.util.Arrays;

/**
 * @author fenqing
 */
public class Topic155 extends DesignTopic {
    public Topic155(String methods, String params) {
        super(methods, params);
    }

    @DesignClass
    public static class MinStack {

        int[] values;
        int[] mins;

        int size;

        public MinStack() {
            this.values = new int[10];
            this.mins = new int[10];
            size = 0;
        }

        private void dilatation(){
            if (size + 1 == values.length) {
                this.values = Arrays.copyOf(this.values, this.values.length << 1);
                this.mins = Arrays.copyOf(this.mins, this.values.length << 1);
            }
        }

        public void push(int val) {
            dilatation();
            this.mins[size] = Math.min(val, getMin());
            this.values[size] = val;
            size++;
        }

        public void pop() {
            size--;
        }

        public int top() {
            return this.values[size - 1];
        }

        public int getMin() {
            if(size == 0) {
                return Integer.MAX_VALUE;
            }
            return mins[size - 1];
        }
    }

    public static void main(String[] args) {
        System.out.println(new Topic155("[\"MinStack\",\"push\",\"push\",\"push\",\"getMin\",\"top\",\"pop\",\"getMin\"]", "[[],[-2],[0],[-3],[],[],[],[]]").run());
    }

}
